<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="40%"
    append-to-body
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item
        label="发票类型"
        prop="type"
      >
        <el-radio-group
          v-model="dataForm.type"
          @change="typeChange"
        >
          <el-radio :label="'1'">公司普通发票</el-radio>
          <el-radio :label="'2'">公司专用发票</el-radio>
          <el-radio :label="'3'">个人普通发票</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        label="发票金额"
        prop="price"
      >
        <el-input
          v-model="dataForm.price"
          placeholder="必填"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="发票号码"
        prop="number"
      >
        <el-input
          v-model="dataForm.number"
          placeholder="必填"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="发票代码"
        prop="code"
      >
        <el-input
          v-model="dataForm.code"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item
        label="发票税额"
        prop="taxAmount"
      >
        <el-input
          v-model="dataForm.taxAmount"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item
        label="发票抬头"
        prop="title"
        :required="this.typeFlag"
      >
        <el-input
          v-model="dataForm.title"
          :placeholder="this.typeFlag?'必填':''"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="地址电话"
        prop="addressPhone"
        :required="this.typeFlag"
      >
        <el-input
          v-model="dataForm.addressPhone"
          :placeholder="this.typeFlag?'必填':''"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="纳税人识别号"
        prop="idNum"
        :required="this.typeFlag"
      >
        <el-input
          v-model="dataForm.idNum"
          :placeholder="this.typeFlag?'必填':''"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="开户行及账号"
        prop="bankAccount"
        :required="this.typeFlag"
      >
        <el-input
          v-model="dataForm.bankAccount"
          :placeholder="this.typeFlag?'必填':''"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="入账人"
        prop="sa"
      >
        <el-autocomplete v-model="dataForm.sa" style="width: 220px;" :fetch-suggestions="querySearchAsync" placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
      </el-form-item>
      <el-form-item
        label="备注"
        prop="note"
      >
        <el-input
          type="textarea"
          v-model="dataForm.note"
          placeholder="备注"
        ></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
        restaurants:[],
      visible: false,
      dataForm: {
        id: 0,
        type: '1',
        number: '',
        price: '',
        code: '',
        taxAmount: '',
        title: '',
        addressPhone: '',
        idNum: '',
        bankAccount: '',
        userId: '',
        note: '',
        createTime: '',
        state: '1',
        fjr:'',
        sa:''
      },
      typeFlag: false,
      dataRule: {
        type: [
          { required: true, message: '发票类型不能为空', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '发票号码不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '发票金额不能为空', trigger: 'blur' }
        ],
        sa: [
          { required: true, message: '入账人不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
     this.getList(3);
  },
  methods: {
    init (id) {
      this.getList(3);
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinpeopleinvoice/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.type = data.tHotelFloorRoomCheckinPeopleInvoice.type
              this.typeChange(this.dataForm.type)
              this.dataForm.number = data.tHotelFloorRoomCheckinPeopleInvoice.number
              this.dataForm.price = data.tHotelFloorRoomCheckinPeopleInvoice.price
              this.dataForm.code = data.tHotelFloorRoomCheckinPeopleInvoice.code
              this.dataForm.taxAmount = data.tHotelFloorRoomCheckinPeopleInvoice.taxAmount
              this.dataForm.title = data.tHotelFloorRoomCheckinPeopleInvoice.title
              this.dataForm.addressPhone = data.tHotelFloorRoomCheckinPeopleInvoice.addressPhone
              this.dataForm.idNum = data.tHotelFloorRoomCheckinPeopleInvoice.idNum
              this.dataForm.bankAccount = data.tHotelFloorRoomCheckinPeopleInvoice.bankAccount
              this.dataForm.userId = data.tHotelFloorRoomCheckinPeopleInvoice.userId
              this.dataForm.note = data.tHotelFloorRoomCheckinPeopleInvoice.note
              this.dataForm.createTime = data.tHotelFloorRoomCheckinPeopleInvoice.createTime
              this.dataForm.state = data.tHotelFloorRoomCheckinPeopleInvoice.state
              this.dataForm.sa = data.tHotelFloorRoomCheckinPeopleInvoice.sa
              this.dataForm.fjr = data.tHotelFloorRoomCheckinPeopleInvoice.fjr
            }
          })
        }
      })
    },
    handleSelect(item) {
       this.dataForm.fjr=item.id;
       var ss= this.dataForm.fjr.split("/");
       this.dataForm.userId=ss[1];
    },
    querySearchAsync(queryString, cb) {
      var restaurants = this.restaurants;
      var results = [];
      for(var i=0;i<restaurants.length;i++){
          var demo= restaurants[i].value;
          var k = demo.indexOf(queryString)=="-1"?"":restaurants[i];
          if(k!=""){
            results.push(restaurants[i]);
          }
      }
     //queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;

      clearTimeout(this.timeout);
        cb(results);
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    getList(type) {
      this.$http({
        url: this.$http.adornUrl('/hotel/trentalgoods/selectroomplist/'+type),
        method: 'post',
      }).then(({
        data
      }) => {
        this.restaurants = data.page;

      });

    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinpeopleinvoice/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'type': this.dataForm.type,
              'number': this.dataForm.number,
              'price': this.dataForm.price,
              'code': this.dataForm.code,
              'taxAmount': this.dataForm.taxAmount,
              'title': this.dataForm.title,
              'addressPhone': this.dataForm.addressPhone,
              'idNum': this.dataForm.idNum,
              'bankAccount': this.dataForm.bankAccount,
              'userId': this.dataForm.userId,
              'note': this.dataForm.note,
              'state': this.dataForm.state
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    typeChange (val) {
      if (val == '2') {
        this.typeFlag = true;
      } else {
        this.typeFlag = false;
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {

}
</style>
